package online.shiming.file.config;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author ShiMing
 * @version 1.0
 * @name Client
 * @description TODO
 * @date 2020/11/13
 */

@Configuration
public class OSSConfig {
    @Value("${oss.end-point}")
    private String endPoint;
    @Value("${oss.accesskey-id}")
    private String accesskeyId;
    @Value("${oss.accesskey-secret}")
    private String accesskeySecret;
    @Value("${oss.bucket-name}")
    private String bucketName;
    private static String key = getKey();

    public static String getKey() {
        LocalDate date = LocalDate.now();
        String[] params = date.toString().split("-");
        key = params[0] + "/" + params[1] + "/" + params[2] + "/";
        return key;
    }

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE )
    public OSS client(){
        return new OSSClientBuilder().build(endPoint, accesskeyId, accesskeySecret);
    }
}
